from base64 import b64encode, decodebytes
from Crypto.Cipher import DES3


def abstract(text):
    return "%s****%s" % (text[:2], text[-2:])


def des3encrypt(key, text):
    des3 = DES3.new(key, DES3.MODE_ECB)
    adds = DES3.block_size - (len(text) % DES3.block_size)
    text = text + (chr(adds) * adds)
    resp = des3.encrypt(text.encode("utf8"))
    return b64encode(resp).decode("utf8")


def des3decrypt(key, text):
    des3 = DES3.new(key, DES3.MODE_ECB)
    resp = decodebytes(text.encode("utf8"))
    resp = des3.decrypt(resp).decode("utf8")
    return resp[0:-ord(resp[-1])]
